home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / Asymptote Demo / Examples / Utility Scripts / polyfit < prev    next >
Text File  |  1994-05-07  |  1KB  |  40 lines

  1. % polyfit order xmin xmax dospline plotline
  2. %
  3. % Calculate a polynomial fit of up to order 5, replace the y vector with the
  4. % fit calculated at each point and (optionally) plot the fit.
  5. %
  6. %        order    - order of polynomial to fit (1,2,3,4,5)
  7. %        xmin     - minimum x to include in fit
  8. %        xmax     - maximum x to include in fit
  9. %           • Note: if xmin = xmax this script won't alter the e vector.
  10. %             You should supply your own weights in this case
  11. %        dospline - if TRUE, then spline the fit before plotting
  12. %     plotline - if TRUE, then plot the fitted function as a line 
  13.  
  14. % If xmax ≠ xmin, then calculate the weights (errors) for the e vector.
  15. if &2 &3 ≠ then rpn <x &2 < <x &3 > + 1 * <x &2 > <x &3 < * 0.0001 * + >e
  16.  
  17. % Perform the fit.
  18. fitxy &1
  19.  
  20. % Calculate the fit on the stack
  21. rpn a0 <x a1 * +
  22. if &1 2 >= then rpn <x dup * a2 * +
  23. if &1 3 >= then rpn <x 3 y^x a3 * +
  24. if &1 4 >= then rpn <x 4 y^x a4 * +
  25. if &1 5 >= then rpn <x 5 y^x a5 * +
  26.  
  27. % Calculate the rms of the fit residuals.
  28. % (This is optional--for speed you can commment these lines out.)
  29. rpn dup <y - rms
  30. set fit_rms v0(1)
  31. rpn drop drop
  32.  
  33. % Put the calculated fit in the y vector:
  34. rpn >y
  35.  
  36. % Calculate a spline and plot a line if requested to do so.
  37. if &4 then spline
  38. if &5 then plotline
  39.  
  40.